package com.atguigu.utils;

import com.atguigu.entity.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

public class TreeHelper {
    public static List<SysMenu> build(List<SysMenu> sysMenus) {
        List<SysMenu> treeMenuList = new ArrayList<>();
        for (SysMenu menuItem : sysMenus) {
            if (menuItem.getParentId().longValue() == 0) {
                treeMenuList.add(buildMenuChildren(sysMenus, menuItem));
            }
        }
        return treeMenuList;
    }

    private static SysMenu buildMenuChildren(List<SysMenu> sysMenu, SysMenu menuItem) {
        menuItem.setChildren(new ArrayList<>());
        for (SysMenu menuItem1 : sysMenu) {
            if (menuItem.getId().longValue() == menuItem1.getParentId().longValue()) {
                menuItem.getChildren().add(buildMenuChildren(sysMenu, menuItem1));
            }
        }
        return menuItem;
    }
}
